package com.hca.training.zk.javabasic.Socket.Socket_Test;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.util.Date;

public class TcpClient {
    public static void  main(String[] arg0) throws IOException, IllegalAccessException {
        String[] ar={"127.0.0.1","8800"};
        runs(ar);
    }
    public static void  runs(String[] arg0) throws IllegalAccessException, IOException {
        //开始参数解析
    if ((arg0.length<2)||(arg0.length>3))
    {
        throw  new IllegalAccessException("参数（s）:<server> <word> [<port>]");
    }
        //------------------转换回馈字符串
    String server=arg0[0];
    //回馈服务器的端口号
    int servPort=(arg0.length==2)? Integer.parseInt(arg0[1]) : 7;
    //------------------创建Tcp套接字
         System.out.println("连接服务器中...     ");
        Socket socket=new Socket(server,servPort);
        System.out.println("连接成功！");
        //接收数据
        InputStream inputStream=socket.getInputStream();
        OutputStream outputStream=socket.getOutputStream();
        //发送数据
        Writer writer = new OutputStreamWriter(outputStream);
        String now_date = new Date().toString();
        writer.write("客户端当前时间为——"+now_date);
        writer.flush();
        socket.shutdownOutput();
        //获取输入流，并读取服务器端的响应信息
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String server_info = null;
        while((server_info=br.readLine())!=null){
            System.out.println(server_info);
        }
    }
}
